//
//  WLMStatusCell.swift
//  WeiBo
//
//  Created by Eric on 2017-01-09.
//  Copyright © 2017 Eric. All rights reserved.
//

import UIKit
import YYWebImage
import SDWebImage
import YYText

private let homeCellMargin: CGFloat = 8
//计算最大的显示宽度
private let maxWidth: CGFloat = ScreenWidth - 2 * homeCellMargin

//定义每张图片的间距
private let pictureCellMargin: CGFloat = 2
//计算每张图片显示的固定宽度
private let itemWidth: CGFloat = (maxWidth - 2 * pictureCellMargin) / 3

class WLMStatusCell: UITableViewCell {

    @IBOutlet weak var headIcon: UIImageView!
    @IBOutlet weak var lbl_name: UILabel!
    @IBOutlet weak var rankView: UIImageView!
    @IBOutlet weak var lbl_time: UILabel!
    @IBOutlet weak var lbl_source: UILabel!
    @IBOutlet weak var verifiIcon: UIImageView!
    @IBOutlet weak var lbl_content: YYLabel!
    @IBOutlet weak var pictureView: WLMPictureCollectionView!
    @IBOutlet weak var pictureWidth: NSLayoutConstraint!
    @IBOutlet weak var pictureHeight: NSLayoutConstraint!
    @IBOutlet weak var pictureFlowLayout: UICollectionViewFlowLayout!
    
    @IBOutlet weak var lbl_retweeted: UILabel!
    @IBOutlet weak var retweetedTopCon: NSLayoutConstraint!
    
    @IBOutlet weak var toolBox: UIStackView!
    
    var viewModel: WLMHomeCellModel? {
        didSet{
            headIcon.yy_setImage(with: viewModel?.headURl, placeholder: #imageLiteral(resourceName: "timeline_image_placeholder"))
            lbl_name.text = viewModel?.status?.user?.screen_name
            lbl_time.text = viewModel?.timeText
            lbl_source.text = viewModel?.sourceText
            lbl_content.text = viewModel?.status?.text
            rankView.image = viewModel?.mbrankIcon
            verifiIcon.image = viewModel?.verifiedImage
            lbl_retweeted?.text = viewModel?.status?.retweeted_status?.text
            
            let count = viewModel?.imageURLS?.count ?? 0
            retweetedTopCon?.constant = (count == 0 ? 0 : homeCellMargin)
            
            let picSize = calculatePictureViewSize(count: count)
            //更新约束
            pictureWidth.constant = picSize.width
            pictureHeight.constant = picSize.height
            
            pictureView.picture_urls = viewModel?.imageURLS
        }
    }
    
    override func awakeFromNib() {
        lbl_content.numberOfLines = 0
        lbl_retweeted?.numberOfLines = 0
        lbl_retweeted?.preferredMaxLayoutWidth = ScreenWidth - homeCellMargin * 2
        lbl_content.preferredMaxLayoutWidth = ScreenWidth - homeCellMargin * 2
        pictureView.backgroundColor = #colorLiteral(red: 1, green: 1, blue: 1, alpha: 1)
        pictureFlowLayout.minimumLineSpacing = pictureCellMargin
        pictureFlowLayout.minimumInteritemSpacing = pictureCellMargin
    }
    
    //计算视图大小
    private func calculatePictureViewSize(count: Int) -> CGSize {
        if count == 0 {
            
            return CGSize.zero
        }
        pictureFlowLayout.itemSize = CGSize(width: itemWidth, height: itemWidth)
        if count == 1 {
            let url = URL(string: viewModel?.imageURLS?.last?.wap360 ?? "")
            let isGifString = (url?.absoluteString.hasSuffix(".gif"))! ? false : true
            let urlString = isGifString ? URL(string: viewModel?.imageURLS?.last?.wap360 ?? "") : URL(string: viewModel?.imageURLS?.last?.bmiddle ?? "")

//            let image = YYWebImageManager.shared().cache?.getImageForKey((url?.absoluteString)!, with: .disk)
            let image = SDWebImageManager.shared().imageCache.imageFromDiskCache(forKey: urlString?.absoluteString)
            
            if let img = image {
                var size = img.size
                print(size)
                size.width = size.width > maxWidth ? maxWidth : size.width
                size.width *= 0.8
                size.height *= 0.8
                pictureFlowLayout.itemSize = size
                return size
            }
        }
        if count == 4 {
            
            let width = itemWidth * 2.001 + pictureCellMargin
            return CGSize(width: width, height: width)
        }
        
        let row = (count - 1) / 3 + 1
        let height = itemWidth * CGFloat(row) + pictureCellMargin * (CGFloat(row) - 1)
        
        return CGSize(width: maxWidth, height: height)
    }
    

    override func setSelected(_ selected: Bool, animated: Bool) {
        super.setSelected(selected, animated: animated)
        
    }
    
}
